﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GrouponDesktop.AbmCliente
{
    public partial class ABMclient : Form
    {
        TextBox tbReg;
        private int overload;
        private int prov;

        public ABMclient()
        {
            InitializeComponent();
            dgv_cli.ReadOnly = true;
            dgv_cli.AllowUserToAddRows = false;
            BDConnection.FillGrid("select nombre , apellido, dni, mail, id_cliente from CodingSpree.TL_CLIENTES", dgv_cli);
            dgv_cli.Rows[0].Selected = true;
            dgv_cli.Columns[0].Width = 119;
            dgv_cli.Columns[1].Width = 119;
            dgv_cli.Columns[2].Width = 80;
            dgv_cli.Columns[3].Width = 175;
            dgv_cli.Columns[4].Visible = false;
            b_acept.Hide();

        }

        public ABMclient(TextBox tb, int tipo, int idProv)
        {
            this.overload = tipo;
            this.tbReg = tb;
            this.prov = idProv;
            InitializeComponent();
            dgv_cli.ReadOnly = true;
            dgv_cli.AllowUserToAddRows = false;
            b_alta.Hide();
            b_baja.Hide();
            b_modif.Hide();

            if (tipo == 0)
            {

                string query = "SELECT nombre , apellido, dni, mail, CLI.id_cliente FROM CodingSpree.TL_CLIENTES CLI, CodingSpree.TL_CUPONxUSUARIO CU, CodingSpree.TL_GROUPON GRO WHERE CLI.id_cliente = CU.id_cliente and CU.id_cupon = GRO.id_cupon and GRO.id_proveedor = " + idProv;
                query = query + "GROUP BY nombre, apellido, dni, mail, Cli.id_cliente ";

                BDConnection.FillGrid(query, dgv_cli);
                if (dgv_cli.Rows.Count > 0)
                {
                    dgv_cli.Rows[0].Selected = true;
                    dgv_cli.Columns[0].Width = 119;
                    dgv_cli.Columns[1].Width = 119;
                    dgv_cli.Columns[2].Width = 80;
                    dgv_cli.Columns[3].Width = 175;
                    dgv_cli.Columns[4].Visible = false;

                }
             }
            else
            {
                gb_filtro.Hide();
                string query = "select codigo_grupon, fecha_compra, cantidad_comprada, CU.id_cuponxusuario from CodingSpree.TL_CUPONxUSUARIO CU, CodingSpree.TL_GROUPON GRO, CodingSpree.TL_ENTREGADOS ENT where CU.id_cupon = GRO.id_cupon and CU.id_cuponxusuario = ENT.id_cuponxusuario and id_cliente = " + tipo;
                query = query + " and GRO.id_proveedor = " + idProv;
                BDConnection.FillGrid(query, dgv_cli);

                
            }
        }

        private void b_alta_Click(object sender, EventArgs e)
        {
            AltaCliente AC = new AltaCliente();
            AC.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (overload == 0)
            {

                BDConnection.FillGrid("EXEC CodingSpree.buscClient '" + tb_f1.Text + "' ,'" + tb_f2.Text + "' ,'" + tb_f4.Text + "' ,'" + tb_f3.Text + "'", dgv_cli);
                dgv_cli.Columns[0].Width = 119;
                dgv_cli.Columns[1].Width = 119;
                dgv_cli.Columns[2].Width = 80;
                dgv_cli.Columns[3].Width = 175;
                dgv_cli.Columns[4].Visible = false;
                if (dgv_cli.Rows.Count > 0)
                {
                    dgv_cli.Rows[0].Selected = true;
                }

            }
            else
            {


                string query = "select codigo_grupon, fecha_compra, cantidad_comprada, CU.id_cuponxusuario from CodingSpree.TL_CUPONxUSUARIO CU, CodingSpree.TL_GROUPON GRO, CodingSpree.TL_ENTREGADOS ENT where CU.id_cupon = GRO.id_cupon and CU.id_cuponxusuario = ENT.id_cuponxusuario and id_cliente = " + overload;
                query = query + " and GRO.id_proveedor = " + this.prov;
                BDConnection.FillGrid(query, dgv_cli);


            }
        }

        private void b_baja_Click(object sender, EventArgs e)
        {
            BajaCliente BC = new BajaCliente((int)dgv_cli.SelectedRows[0].Cells["id_cliente"].Value);
            BC.Show();
        }

        private void b_modif_Click(object sender, EventArgs e)
        {
            ModifCliente MC = new ModifCliente((int)dgv_cli.SelectedRows[0].Cells["id_cliente"].Value);
            MC.Show();
        }

        private void b_acept_Click(object sender, EventArgs e)
        {
            if ( dgv_cli.SelectedRows.Count > 0)
            {
                if (this.overload == 0)
                {
                    tbReg.Text = dgv_cli.SelectedRows[0].Cells["id_cliente"].Value.ToString();
                }
                else
                {
                    tbReg.Text = dgv_cli.SelectedRows[0].Cells["id_cuponxusuario"].Value.ToString();
                } 
            this.Hide();
            }
        }

       

    }
}
